package com.yswee.mallnavi.view.coupon.hor;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import com.yswee.framework.widget.adapterview.MHorizontalListView;
import com.yswee.framework.widget.adapterview.withmodel.MListView;
import com.yswee.framework.widget.layoutview.ILayoutView;
import com.yswee.mallnavi.entity.info.CouponEntity;
import com.yswee.mallnavi.view.coupon.IListView;
import com.yswee.mallnavi.view.coupon.list.ListItem;

import java.util.ArrayList;

public class HListView extends MHorizontalListView<CouponEntity> {
	
	private IListView.IActionCallback mActionCallback;

	public void setActionCallback(IListView.IActionCallback c) {
		mActionCallback = c;
	}

	private IListView.IActionCallback mActionCallbackProxy = new IListView.IActionCallback() {
		@Override
		public void onInfoClicked(View v, CouponEntity coupon) {
			if (mActionCallback != null) {
				mActionCallback.onInfoClicked(v, coupon);
			}
		}
	};
	
	public HListView(Context context) {
		super(context);
	}
	
	public HListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	@Override
	protected int getListViewType() {
		return MListView.LISTVIEW_TYPE_VIEW;
	}
	
	@Override
	protected ILayoutView<CouponEntity> getLayoutView(int position, CouponEntity t, int itemViewType) {
		HListItem listitem = new HListItem(mContext);
		listitem.setActionCallback(mActionCallbackProxy);
		return listitem;
	}

	@Override
	public void setDataSource(ArrayList<CouponEntity> array) {
		if (array == null) {
			return;
		}
		if (array != null) {
			// 分为2行
			if (array.size() > 0) {
				ArrayList<CouponEntity> lstnew = new ArrayList<>();
				for (int i = 0; i < array.size(); i+= 2) {
					if (i + 1 < array.size()) {
						array.get(i).bottom = array.get(i + 1);
					} else {
						array.get(i).bottom = null;
					}
					lstnew.add(array.get(i));
				}
				array = lstnew;
			}
		}
		super.setDataSource(array);
	}
}